package first;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.StringTokenizer;

public class SentFilter 
{
	private HashMap<String, Integer> positiv = new HashMap<String, Integer>();
	private HashMap<String, Integer> negativ = new HashMap<String, Integer>();
	private HashMap<String, Integer> ratingList = new HashMap<String, Integer>();
	private ArrayList<String> waitList = new ArrayList<String>();
	private ArrayList<Tweet> tweets;
	private StringTokenizer st;
	
	public SentFilter()
	{
		this.read();
	}
	
	public ArrayList<Tweet> getSentiment(ArrayList<Tweet> tweets2) throws IOException
	{
		this.tweets = tweets2;
		int rating = 0;
		String token;
		
		for(int i = 0; i < tweets.size(); i++)
        {
        	this.st = new StringTokenizer(tweets.get(i).getTextSent());
        	
        	while(st.hasMoreTokens())
        	{
        		token = st.nextToken();
        		rating += checkSent(token);
        	}
        	
        	this.tweets.get(i).setRating(rating);
        	
        	if(rating != 0)
    		{
    			checkRatingList(rating);
    		}
        	
        	waitList.clear();
        	rating = 0;
        	token = "";
        }
		
		write();
		
		return this.tweets;
	}
	
	public int checkSent(String w)
	{
		if(this.positiv.get(w) != null)
		{
			return positiv.get(w);
		}
		else if(this.negativ.get(w) != null)
		{
			return negativ.get(w);
		}
		else
		{
			if(this.ratingList.get(w) == null)
			{
				ratingList.put(w, 0);
			}
			
			this.waitList.add(w);
			return 0;
		}
	}
	
	public void checkRatingList(int r)
	{
		String word;
		int value;
		
		if(!waitList.isEmpty())
		{
			for(int i = 0; i < waitList.size(); i++)
			{
				if(ratingList.get(waitList.get(i)) != null)
				{
					word = waitList.get(i);
					value = ratingList.get(waitList.get(i));
					
					if(r > 0)
					{
						if(ratingList.get(waitList.get(i)) < 100)
						{
							ratingList.put(word, (value+1));
						}
						else
						{
							positiv.put(waitList.get(i), 1);
							ratingList.remove(waitList.get(i));
						}
					}
					else if(r < 0)
					{
						if(ratingList.get(waitList.get(i)) > -100)
						{
							ratingList.put(word, (value-1));
						}
						else
						{
							negativ.put(waitList.get(i), -1);
							ratingList.remove(waitList.get(i));
						}
					}
				}
			}
		}
	}
	
	
	public void read()
	{
		try 
		{
			BufferedReader br;
			
			String line = "";
			String word = "";
			String value = "";
			
			br = new BufferedReader(new FileReader("positiv.txt"));
			while ((line = br.readLine()) != null) 
			{
				this.st = new StringTokenizer(line);
        		word = st.nextToken();
        		value = st.nextToken();
        		positiv.put(word, Integer.parseInt(value));
			}
			br.close();
			
			br = new BufferedReader(new FileReader("negativ.txt"));
			while ((line = br.readLine()) != null) 
			{
				this.st = new StringTokenizer(line);
				word = st.nextToken();
				value = st.nextToken();
	        	negativ.put(word, Integer.parseInt(value));
			}
			br.close();
			
			br = new BufferedReader(new FileReader("ratingList.txt"));
			while ((line = br.readLine()) != null) 
			{
				this.st = new StringTokenizer(line);
				word = st.nextToken();
				value = st.nextToken();
	        	ratingList.put(word, Integer.parseInt(value));
			}
			br.close();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	
	public void write() throws IOException
	{
        BufferedWriter out; 
        
        out = new BufferedWriter(new FileWriter("positiv.txt"));

        for (String elem : positiv.keySet())
        {
        	out.write(elem +" "+ positiv.get(elem));
        	out.newLine();
        }
        out.close();
        
        out = new BufferedWriter(new FileWriter("negativ.txt"));

        for (String elem : negativ.keySet())
        {
        	out.write(elem +" "+ negativ.get(elem));
        	out.newLine();
        }
        out.close();
        
        out = new BufferedWriter(new FileWriter("ratingList.txt"));

        for (String elem : ratingList.keySet())
        {
        	out.write(elem +" "+ ratingList.get(elem));
        	out.newLine();
        }
        out.close();
	}
}
